package com.lhkj.ct.admin.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.config.FileServerConfig;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.base.utils.file.FileUploadUtils;
import com.lhkj.ct.base.utils.file.MimeTypeUtils;
import com.lhkj.ct.meta.admin.model.entity.TblEmployee;
import com.lhkj.ct.meta.admin.model.vo.UserProfile;
import com.lhkj.ct.meta.admin.service.SysEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 个人信息 业务处理
 *
 *
 */
@RestController
@RequestMapping("api/system/user/profile")
@Api(tags = "个人中心")
@ApiSupport(order = 0)
public class SysProfileController{

    @Resource
    private SysEmployeeService userService;

    /**
     * 个人信息
     */
    @ApiOperation("个人信息")
    @ApiOperationSupport(order = 1)
    @GetMapping("getInfo")
    public ActionResult<UserProfile> profile() {
        return ActionResult.success(userService.getUserProfile(ShiroUtil.getAuthUserInfo().getUserId()));
    }

    /**
     * 修改用户
     */
    @ApiOperation("修改用户")
    @ApiOperationSupport(order = 2)
    @Log(title = "个人信息", actionType = ActionType.UPDATE)
    @PutMapping
    public ActionResult<Void> updateProfile(@RequestBody TblEmployee user) {
        if (userService.updateUserProfile(user) > 0) {
            // 更新缓存用户信息
//            tokenService.setLoginUser(loginUser);
            return ActionResult.success();
        }
        return ActionResult.fail("修改个人信息异常，请联系管理员");
    }

    /**
     * 重置密码
     */
    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 3)
    @Log(title = "个人信息", actionType = ActionType.UPDATE)
    @PutMapping("/updatePwd")
    public ActionResult<Void> updatePwd(@RequestParam String oldPassword,@RequestParam String newPassword) {
        if (userService.resetUserPwd(oldPassword, newPassword) > 0) {
            // 更新缓存用户密码
//            loginUser.getUser().setPassword(SecurityUtils.encryptPassword(newPassword));
//            tokenService.setLoginUser(loginUser);
            return ActionResult.success();
        }
        return ActionResult.fail("修改密码异常，请联系管理员");
    }

    /**
     * 头像上传
     */
    @ApiOperation("上传头像")
    @ApiOperationSupport(order = 4)
    @Log(title = "用户头像", actionType = ActionType.UPDATE)
    @PostMapping("/avatar")
    public ActionResult<?> avatar(@RequestParam("avatar") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(FileServerConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (userService.updateUserAvatar(ShiroUtil.getAuthUserInfo().getUserId(), avatar)) {
                Map<String, String> map = new HashMap<>();
                map.put("imgUrl", avatar);
                return ActionResult.success(map);
            }
        }
        return ActionResult.fail("上传图片异常，请联系管理员");
    }
}
